<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务编号" prop="tasksNum">
        <el-input
          v-model="queryParams.tasksNum"
          placeholder="请输入任务编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务标题" prop="tasksName">
        <el-input
          v-model="queryParams.tasksName"
          placeholder="请输入任务标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="发布者ID" prop="publisherid">-->
<!--        <el-input-->
<!--          v-model="queryParams.publisherid"-->
<!--          placeholder="请输入发布者ID"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="认领类型" prop="isMany">
        <el-select v-model="queryParams.isMany" placeholder="请选择认领类型" clearable size="small">
          <el-option
            v-for="dict in isManyOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务状态" prop="tasksType">
        <el-select v-model="queryParams.tasksType" placeholder="请选择任务状态" clearable size="small">
          <el-option
            v-for="dict in tasksTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发布积分" prop="releaseScore">
        <el-input
          v-model="queryParams.releaseScore"
          placeholder="请输入发布积分"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="认领积分" prop="claimScore">
        <el-input
          v-model="queryParams.claimScore"
          placeholder="请输入认领积分"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="完成时间" prop="completionTime">-->
<!--        <el-date-picker clearable size="small" style="width: 200px"-->
<!--          v-model="queryParams.completionTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="选择完成时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="任务图片" prop="tasksImg">-->
<!--        <el-input-->
<!--          v-model="queryParams.tasksImg"-->
<!--          placeholder="请输入任务图片"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tasks:tasks:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tasks:tasks:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tasks:tasks:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tasks:tasks:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tasksList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="任务编号" align="center" prop="tasksNum" />
      <el-table-column label="任务标题" align="center" prop="tasksName" />
      <el-table-column label="发布者名称" align="center" prop="publisherName" />
      <el-table-column label="认领者名称" align="center" prop="claimerName" />
<!--      <el-table-column label="任务内容" align="center" prop="tasksContent" />-->
      <el-table-column label="认领类型" align="center" prop="isMany" :formatter="isManyFormat" />
      <el-table-column label="任务状态" align="center" prop="tasksType" :formatter="tasksTypeFormat" />
      <el-table-column label="发布积分" align="center" prop="releaseScore" />
      <el-table-column label="认领积分" align="center" prop="claimScore" />
      <el-table-column label="发布时间" align="center" prop="releaseTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.releaseTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="center" prop="completionTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.completionTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务图片" align="center" prop="tasksImg" >
        <template slot-scope="scope">
          <img
            :src="baseUrl+scope.row.tasksImg"
            style="display: block; max-width: 100%; margin: 0 auto"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.tasksType == 0"
            icon="el-icon-edit"
            @click="goReleaseTasks(scope.row)"
            v-hasPermi="['tasks:tasks:add']"
          >发布</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.tasksType == 1"
            icon="el-icon-plus"
            @click="goClaimTasks(scope.row)"
            v-hasPermi="['tasks:tasks:claim']"
          >认领</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            v-if="scope.row.tasksType == 2"
            @click="handleReport(scope.row)"
            v-hasPermi="['tasks:tasks:progress']"
          >提报</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tasks:tasks:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tasks:tasks:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="任务编号" prop="tasksNum">-->
<!--          <el-input v-model="form.tasksNum" placeholder="请输入任务编号" />-->
<!--        </el-form-item>-->
        <el-form-item label="任务标题" prop="tasksName">
          <el-input v-model="form.tasksName" placeholder="请输入任务标题" />
        </el-form-item>
<!--        <el-form-item label="发布者ID" prop="publisherid">-->
<!--          <el-input v-model="form.publisherid" placeholder="请输入发布者ID" />-->
<!--        </el-form-item>-->
        <el-form-item label="任务内容" prop="tasksContent">
          <editor v-model="form.tasksContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="认领类型" prop="isMany">
          <el-select v-model="form.isMany" placeholder="请选择认领类型">
            <el-option
              v-for="dict in isManyOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.id" label="任务状态" prop="tasksType">
          <el-select v-model="form.tasksType" placeholder="请选择任务状态">
            <el-option
              v-for="dict in tasksTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发布积分" prop="releaseScore">
          <el-input v-model="form.releaseScore" placeholder="请输入发布积分" />
        </el-form-item>
        <el-form-item label="认领积分" prop="claimScore">
          <el-input v-model="form.claimScore" placeholder="请输入认领积分" />
        </el-form-item>
        <el-form-item label="预计完成时间" prop="completionTime">
          <el-date-picker
            v-model="form.completionTime"
            format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" clearable style="width: 100%" type="datetime" placeholder="请输入预计完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="任务图片" prop="tasksImg">
          <ImageUpload v-model="form.tasksImg"  />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改进度提报对话框 -->
    <el-dialog :title="title" :visible.sync="openProgress" width="500px" append-to-body>
      <el-form ref="progressForm" :model="progressForm" :rules="progressRules" label-width="80px">
        <el-form-item label="任务名称" prop="tasksName">
          <el-input v-model="progressForm.tasksName" placeholder="请输入任务名称"  disabled/>
        </el-form-item>
        <el-form-item label="任务编号" prop="tasksNum">
          <el-input v-model="progressForm.tasksNum" placeholder="请输入任务编号" disabled />
        </el-form-item>
        <el-form-item label="认领者名称" prop="claimerName">
          <el-input v-model="progressForm.claimerName" placeholder="请输入认领者名称" disabled/>
        </el-form-item>
        <el-form-item label="进度描述" prop="progressDetal">
          <el-input v-model="progressForm.progressDetal" placeholder="请输入进度描述" />
        </el-form-item>
        <el-form-item label="进度占比" prop="progressNum">
          <el-input v-model.number="progressForm.progressNum" placeholder="请输入进度占比"  type="number" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitProgressForm">确 定</el-button>
        <el-button @click="cancelReport">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTasks,
  getTasks,
  delTasks,
  addTasks,
  updateTasks,
  exportTasks,
  claimTasks,
  progressReport,
  releaseTasks
} from '@/api/tasks/tasks'
import Editor from '@/components/Editor';

import ImageUpload from "@/components/ImageUpload"
export default {
  name: "Tasks",
  components: { Editor,ImageUpload },
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目任务表格数据
      tasksList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      openProgress: false,
      // 认领类型字典
      isManyOptions: [],
      // 任务状态字典
      tasksTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tasksNum: null,
        tasksName: null,
        publisherid: null,
        tasksContent: null,
        isMany: null,
        tasksType: null,
        releaseScore: null,
        claimScore: null,
        completionTime: null,
        tasksImg: null
      },
      // 表单参数
      form: {},
      // 表单参数
      progressForm: {},
      // 表单校验
      rules: {
        tasksNum: [
          { required: true, message: "任务编号不能为空", trigger: "blur" }
        ],
        tasksName: [
          { required: true, message: "任务标题不能为空", trigger: "blur" }
        ],
        publisherid: [
          { required: true, message: "发布者ID不能为空", trigger: "blur" }
        ],
        isMany: [
          { required: true, message: "认领类型不能为空", trigger: "change" }
        ],
        tasksType: [
          { required: true, message: "任务状态不能为空", trigger: "change" }
        ],
        releaseScore: [
          { required: true, message: "发布积分不能为空", trigger: "blur" }
        ],
        claimScore: [
          { required: true, message: "认领积分不能为空", trigger: "blur" }
        ],
        tasksImg: [
          { required: true, message: "任务图片不能为空", trigger: "blur" }
        ]
      },
      progressRules: {
        progressNum: [
          { required: true, message: "进度占比不能为空", trigger: "blur" },
          {
            type: 'number',
            min: 0,
            max: 100,
            message: '进度占比区间为0-100',
            trigger: 'blur'
          }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("qs_tasks_is_many").then(response => {
      this.isManyOptions = response.data;
    });
    this.getDicts("qs_tasks_type").then(response => {
      this.tasksTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询项目任务列表 */
    getList() {
      this.loading = true;
      listTasks(this.queryParams).then(response => {
        this.tasksList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 认领类型字典翻译
    isManyFormat(row, column) {
      return this.selectDictLabel(this.isManyOptions, row.isMany);
    },
    // 任务状态字典翻译
    tasksTypeFormat(row, column) {
      return this.selectDictLabel(this.tasksTypeOptions, row.tasksType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancelReport() {
      this.openProgress = false;
      this.resetProgress();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tasksNum: null,
        tasksName: null,
        createTime: null,
        publisherid: null,
        tasksContent: null,
        isMany: null,
        tasksType: null,
        releaseScore: null,
        claimScore: null,
        completionTime: null,
        tasksImg: null
      };
      this.resetForm("form");
    },
    // 表单重置
    resetProgress() {
      this.progressForm = {
        id: null,
        tasksClaimId: null,
        tasksId: null,
        tasksName: null,
        tasksNum: null,
        claimerName: null,
        createTime: null,
        progressDetal: null,
        progressNum: null
      };
      this.resetForm("progressForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      console.log(row)
      const id = row.id || this.ids
      getTasks(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目任务";
      });
    },
    /** 提报按钮操作 */
    handleReport(row) {
        this.resetProgress();
        this.progressForm.tasksId = row.id;
        this.progressForm.tasksName = row.tasksName;
        this.progressForm.id = null;

        this.openProgress = true;
        this.title = "提报项目任务进度";
    },
    /** 发布任务 */
    goReleaseTasks(row) {
      this.$confirm('是否确认发布项目任务名称为"' + row.tasksName + '"的任务?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        releaseTasks(row.id);
      }).then(() => {
        this.getList();
        this.msgSuccess("发布成功");
      })

    },
    /** 认领任务 */
    goClaimTasks(row) {
      this.$confirm('是否确认认领项目任务名称为"' + row.tasksName + '"的任务?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        claimTasks(row.id);
      }).then(() => {
        this.getList();
        this.msgSuccess("认领成功");
      })

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTasks(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTasks(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitProgressForm() {
      this.$refs["progressForm"].validate(valid => {
        if (valid) {
          progressReport(this.progressForm).then(response => {
            this.msgSuccess("提报成功");
            this.openProgress = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除项目任务编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTasks(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有项目任务数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTasks(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
